package com.demoweb.tetris;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demoweb.model.dao.BoardDao;
import com.demoweb.model.dto.Board;

@WebServlet("/board/view0.action")
public class CommentViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		
//		//로그인 상태가 아닌 경우 로그인 페이지로 이동
//		if (request.getSession().getAttribute("loginuser") == null) {
//			response.sendRedirect(
//				"/demoweb/account/loginform.action?" + 
//				"returnuri=" + request.getRequestURI());
//			return;
//		}
		
		//요청 정보에서 내용을 표시할 글번호를 읽고 변수에 저장
		//(없으면 목록으로 이동)
		String groupNo = request.getParameter("groupno");
		if (groupNo == null || groupNo.length() == 3) {
			response.sendRedirect("/demoweb/tetris/list0.action");
			return;
		}
		int no = Integer.parseInt(groupNo);
		
		//데이터베이스에서 데이터 조회
		BoardDao dao = new BoardDao();
		Board board = dao.getBoardByBoardNo(3);
		
		//조회 실패하면 목록으로 이동
		if (board == null) {
			response.sendRedirect("/demoweb/tetris/list0.action");
			return;
		}
		/*
		dao.increaseBoardReadCount(board.getBoardNo());
		board.setReadCount(board.getReadCount() + 1);*/
		
		//조회된 데이터를 jsp 처리할 수 있도록 request 객체에 저장
		request.setAttribute("board", board);
		
		RequestDispatcher dispatcher = 
			request.getRequestDispatcher("/WEB-INF/views/tetris/comment.jsp");

		dispatcher.forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
